<template>
  <div class="home-container">
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        class="search-btn"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <van-tabs class="channel-tabs" v-model="active">
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <article-list :channel="channel" />
      </van-tab>

      <div slot="nav-right" class="wap-nav-placeholder"></div>
      <div
        slot="nav-right"
        @click="isChannelEditShow = true"
        class="wap-nav-wrap"
      >
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>

    <van-popup
      v-model="isChannelEditShow"
      position="bottom"
      class="channel-edit-popup"
      closeable
      close-icon-position="top-left"
      get-container="body"
      style="height: 100%"
    >
    <channel-edit :user-channels="channels" @close="isChannelEditShow=false" @update-active="active=$event" :active="active"/>
    </van-popup>
  </div>
</template>
<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/article-list.vue";
import ChannelEdit from './components/channel-edit.vue'
import {mapState} from 'vuex'
import {getItem} from '@/utils/storage'
export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    ChannelEdit
  },
  created() {
    this.loadChannels();
  },
  computed:{
    ...mapState(['user'])
  },
  data() {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false, //控制编辑频道的显示状态
    };
  },
  methods: {
    async loadChannels() {
      let channels=[]
      if(this.user){
        // 已登录请求获取线上的用户频道列表数据
        const{data}=await getUserChannels()
        channels=data.data.channels
      }else{
        // 没有登录，判断是否有本地存储的频道列表数据
        const  localChannels=getItem('user-channels')
        // 如果有本地存储的频道列表则使用
        if(localChannels){
          channels=localChannels
        }else{
          const { data } = await getUserChannels();
          channels=data.data.channels
        }
      }
      // const { data } = await getUserChannels();
      this.channels = channels
    },
    // onUpdateActive(index){
    //     this.active=index
    // }
  },
};
</script>
<style scoped lang="less">
.home-container {
  /deep/.van-nav-bar__title {
    max-width: none;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    background: #5babfb;
    border: none;
    .van-icon {
      font-size: 16px;
    }
    .van-button__text {
      font-size: 14px;
    }
  }
  .channel-tabs {
    /deep/.van-tab {
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/.van-tabs__line {
      width: 15px !important;
      height: 3px;
      background-color: #3296fa;
      bottom: 20px;
    }
  }
  .wap-nav-placeholder{
      width: 33px;
      flex-shrink: 0;
  }
  .wap-nav-wrap {
    position: fixed;
    right: 0;
    opacity: 0.9;
    width: 33px;
    height: 43px;
    line-height: 44px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    .van-icon {
      font-size: 24px;
    }
    &:before {
      content: "";
      width: 1px;
      height: 29px;
      background: url("./竖线.png") no-repeat;
      background-size: cover;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
 
}
</style>